/**
 * AluiCommons -  http://code.google.com/p/alui-toolbox/
 * Copyright (c) 2008-2009
 * by Fabien Sanglier
 * 
 * This file is part of AluiCommons.
 * 
 * AluiCommons is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * AluiCommons is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with AluiCommons.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

using System;
using System.Data;
using System.Configuration;
using com.plumtree.server;
using com.plumtree.openfoundation.util;
using com.plumtree.openlog;
using com.plumtree.server.impl.portlet;
using System.Collections;
using com.plumtree.server.impl.core;
using AluiServerLib.Business;

namespace AluiServerLib.Models
{
    public class PTPortletModel
    {
        private static OpenLogger log = OpenLogService.GetLogger(
            "AluiServerLib",
            "AluiServerLib.Models.PTNavigationModel"); 
        
        private IPTSession m_ptSession = null;
		private IPTObjectManager m_ptGadgetManager = null;
        private PTNavigationModel m_ptNav = null;

        public PTPortletModel(IPTSession ptSession)
		{
			m_ptSession = ptSession;
            m_ptGadgetManager = ptSession.GetGadgets();
            m_ptNav = new PTNavigationModel(m_ptSession);
		}

		public IPTSession PTSession
		{
			get
			{
				return m_ptSession;
			}
		}

        public void Release()
        {
            m_ptSession = null;
            m_ptGadgetManager = null;
            m_ptNav.Release();
            m_ptNav = null;
        }

        public ArrayList GetPortletsOnAllCommunities(bool inflate)
        {
            log.Info("BEGIN GetPortletsOnAllCommunities");
            ArrayList arrCommunities = m_ptNav.GetAvailableCommunities();
            ArrayList arrPortlets = null;
            ArrayList arrTemp = null;
            if (null != arrCommunities)
            {
                arrPortlets = new ArrayList(); 
                foreach (ALUICommunity comm in arrCommunities)
                {
                    log.Debug("Scanning Community : " + comm.GetName() + ".");

                    arrTemp = GetPortletsOnCommunity(comm.GetObjectID(), inflate);
                    if (null != arrTemp)
                        arrPortlets.AddRange(arrTemp); 
                }
            }
            log.Info("END GetPortletsOnAllCommunities");
            return arrPortlets;
        }

        public ArrayList GetPortletsOnCommunities(int[] commids, bool inflate)
        {
            log.Info("BEGIN GetPortletsOnCommunities");
            ArrayList arrPortlets = null;
            ArrayList arrTemp = null;
            if (null != commids)
            {
                arrPortlets = new ArrayList();

                foreach (int commid in commids)
                {
                    arrTemp = GetPortletsOnCommunity(commid, inflate);
                    if (null != arrTemp)
                        arrPortlets.AddRange(arrTemp); 
                }
            }
            log.Info("END GetPortletsOnCommunities");
            return arrPortlets;
        }

        public ArrayList GetPortletsOnAllMyPages(bool inflate)
        {
            log.Info("BEGIN GetPortletsOnAllMyPages");
            ArrayList arrPortlets = null;
            ArrayList arrTemp = null;

            XPHashtable arrMyPages = m_ptNav.GetAllMyPages();
            if (arrMyPages.GetSize() > 0)
            {
                arrPortlets = new ArrayList();
                IXPEnumerator enumMyPages = arrMyPages.GetElements();
                while (enumMyPages.MoveNext())
                {
                    ALUIPage mypage = (ALUIPage)enumMyPages.GetCurrent();
                    arrTemp = GetPortletsOnPage(mypage.GetObjectID(), inflate);
                    if (null != arrTemp)
                        arrPortlets.AddRange(arrTemp);
                }
            }
            log.Info("END GetPortletsOnAllMyPages");
            return arrPortlets;
        }

        public ArrayList GetPortletsOnCommunity(int communityid, bool inflate)
        {
            log.Info("BEGIN GetPortletsOnCommunity");
            ArrayList arrPortlets = null;
            ArrayList arrTemp = null;
            XPHashtable arrCommunityPages = m_ptNav.GetCommunityPages(communityid);
            if (arrCommunityPages.GetSize() > 0)
            {
                arrPortlets = new ArrayList(); 
                IXPEnumerator enumCommunityPages = arrCommunityPages.GetElements();

                while (enumCommunityPages.MoveNext())
                {
                    ALUIPage commPage = (ALUIPage)enumCommunityPages.GetCurrent();
                    arrTemp = GetPortletsOnPage(commPage.GetObjectID(), inflate);
                    if (null != arrTemp)
                        arrPortlets.AddRange(arrTemp);
                }
            }

            log.Info("END GetPortletsOnCommunity");
            return arrPortlets;
        }

        public ArrayList GetPortletsOnPages(int[] pageids, bool inflate)
        {
            log.Info("BEGIN GetPortletsOnPages");
            ArrayList arrPortlets = null;
            ArrayList arrTemp = null;
            if (null != pageids)
            {
                arrPortlets = new ArrayList();
                foreach (int pageid in pageids)
                {
                    arrTemp = GetPortletsOnPage(pageid, inflate);
                    if (null != arrTemp)
                        arrPortlets.AddRange(arrTemp);
                }
            }
            log.Info("END GetPortletsOnPages");
            return arrPortlets;
        }

        public ArrayList GetPortletsOnPage(int pageid, bool inflate)
        {
            log.Info("BEGIN GetPortletsOnPage");

            ArrayList arrPortlets = null;

            ALUIPage ptpage = new ALUIPage();
            ptpage.SetObjectID(pageid);
            ptpage.Inflate(m_ptSession, false);

            //get the page info for this community page id
            IPTQueryResult qrPortlets = ptpage.QueryPortlets();
            if (qrPortlets.RowCount() > 0)
            {
                arrPortlets = new ArrayList(); 
                for (int i = 0; i < qrPortlets.RowCount(); i++)
                {
                    ALUIPortlet ptportlet = null;
                    if (inflate)
                    {
                        ptportlet = new ALUIPortlet(qrPortlets.ItemAsInt(i, PT_PROPIDS.PT_PROPID_OBJECTID));
                        ptportlet.Inflate(m_ptSession, false);
                    }
                    else
                    {
                        ptportlet = new ALUIPortlet(qrPortlets.ItemAsInt(i, PT_PROPIDS.PT_PROPID_OBJECTID));
                        ptportlet.SetName(qrPortlets.ItemAsString(i, PT_PROPIDS.PT_PROPID_NAME));
                        ptportlet.SetDescription(qrPortlets.ItemAsString(i, PT_PROPIDS.PT_PROPID_DESCRIPTION));
                    }

                    arrPortlets.Add(ptportlet);
                }
            }

            log.Info("END GetPortletsOnPage");
            return arrPortlets;
        }

        public ArrayList GetPortlets(int[] portletids, bool inflate)
        {
            log.Info("BEGIN GetPortlets");
            ArrayList arrPortlets = null;
            if (null != portletids)
            {
                arrPortlets = new ArrayList();
                
                foreach (int portletid in portletids)
                {
                    ALUIPortlet ptportlet = null;
                    if (inflate)
                    {
                        ptportlet = new ALUIPortlet(portletid);
                        ptportlet.Inflate(m_ptSession, false);
                    }
                    else
                    {
                        ptportlet = new ALUIPortlet(portletid);
                    }

                    arrPortlets.Add(ptportlet);
                }
            }
            log.Info("END GetPortlets");
            return arrPortlets;
        }
    }
}
